using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace OrigPacman
{
    //================================================
    //Main class Map
    //================================================
    class Map
    {
        Texture2D background;
        Texture2D point;
        Waypoint tmp;
        List<Waypoint> currentWaypoints;
        //List<SuperPellet> pelletList;
        List<Pellet> pelletList;
        StreamReader file;
        long frameCount = 0;
        long pressed = 0;
        bool drawWaypoints = false;
        Texture2D bigPellet;
        Texture2D smallPellet;

        public Map(GraphicsDevice device)
        {
            tmp = new Waypoint();
            pelletList = new List<Pellet>();
            LoadLevel1();
            setupPellets();
        }

        public void LoadContent(ContentManager content)
        {
            background = content.Load<Texture2D>("images/background1");
            point = content.Load<Texture2D>("images/point");
            bigPellet = content.Load<Texture2D>("images/pellet");
            smallPellet = content.Load<Texture2D>("images/smallPellet");

        }

        public void Update(float elapsed, Vector2 pacPosition)
        {
            CheckPelletCollision(pacPosition);
        }

        public void Render(ref SpriteBatch sprite)
        {
            frameCount++;
            if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.F1) && pressed < frameCount-5)
            {
                pressed = frameCount;
                drawWaypoints = !drawWaypoints;
            }
            sprite.Draw(background, new Vector2(65.0f, 65.0f), new Rectangle(0, 0, 512,512), Color.White, 0.0f, new Vector2(16.0f, 16.0f), 1.0f, SpriteEffects.None, 0.1f);
            int x = 0;
            int y = 0;
            if (drawWaypoints)
            {
                foreach (Waypoint a in currentWaypoints)
                {
                    x = (int)a.Position.X;
                    y = (int)a.Position.Y;
                    sprite.Draw(point, new Rectangle(x - 8, y - 8, 16, 16), Color.White);

                }
            }
            foreach (Pellet a in pelletList)
            {
                x = (int)a.Position.X;
                y = (int)a.Position.Y;
                sprite.Draw(smallPellet, new Rectangle(x, y, 32, 32), Color.White);
            }
        }

        private void LoadLevel1()
        {
            file = new StreamReader("level1.txt");
            tmp = new Waypoint();
            currentWaypoints = new List<Waypoint>();
            tmp.Left = 8;
            tmp.Right = 47;
            tmp.Position = new Vector2(289, 385);
            currentWaypoints.Add(tmp);
            string nextString = file.ReadLine();
            if (nextString.Contains("Waypoint"))
            {
                while (nextString != "END")
                {
                    //Reset all positions
                    tmp = new Waypoint();
                    if (nextString.Contains("Vector"))
                    {
                        int x, y;
                        int beginningOfX = nextString.IndexOf('x') + 1;
                        int beginningOfY = nextString.IndexOf('y', beginningOfX) + 1;

                        x = Convert.ToInt32(nextString.Substring(beginningOfX, beginningOfY - beginningOfX - 1));
                        y = Convert.ToInt32(nextString.Substring(beginningOfY));
                        tmp.Position = new Vector2(x+48+16, y+48+16);
                    }
                    nextString = file.ReadLine();
                    int nextNode = 0;
                    while (!nextString.Contains("Waypoint") && nextString != "END")
                    {
                        if (nextString.Contains("Up"))
                        {
                            nextNode = Convert.ToInt32(nextString.Substring(3));
                            tmp.Up = nextNode;
                        }
                        if (nextString.Contains("Down"))
                        {
                            nextNode = Convert.ToInt32(nextString.Substring(5));
                            tmp.Up = nextNode;
                        }
                        if (nextString.Contains("Left"))
                        {
                            nextNode = Convert.ToInt32(nextString.Substring(5));
                            tmp.Left = nextNode;
                        }
                        if (nextString.Contains("Right"))
                        {
                            nextNode = Convert.ToInt32(nextString.Substring(6));
                            tmp.Right = nextNode;
                        }
                        nextString = file.ReadLine();
                    }
                    currentWaypoints.Add(tmp);
                }//End while
            }//End if
            file.Close();
        }//End LoadLevel1

        public List<Waypoint> getWaypoints()
        {
            return currentWaypoints;
        }

        private void setupPellets()
        {
            SuperPellet tmp = new SuperPellet();
            //Create four Super Pellets
            tmp.Position = new Vector2(65, 65);
            pelletList.Add(tmp);
            tmp = new SuperPellet();
            tmp.Position =new Vector2(512, 65);
            pelletList.Add(tmp);
            tmp = new SuperPellet();
            tmp.Position =new Vector2(65, 512);
            pelletList.Add(tmp);
            tmp = new SuperPellet();
            tmp.Position =new Vector2(512, 512);
            pelletList.Add(tmp);
            Pellet regPellet = new Pellet();
            //Create Pellet;
            for (int i = 0; i < 13; i++)
            {
                regPellet.Position =new Vector2(65, (i * 32) + 97);
                pelletList.Add(regPellet);
                regPellet = new Pellet();
                regPellet.Position =new Vector2(512, (i * 32) + 97);
                pelletList.Add(regPellet);
                regPellet = new Pellet();
                regPellet.Position =new Vector2((i * 32) + 97, 512);
                pelletList.Add(regPellet);
                regPellet = new Pellet();
                regPellet.Position =new Vector2((i * 31) + 101, 370);
                pelletList.Add(regPellet);
                regPellet = new Pellet();
                regPellet.Position =new Vector2((i * 31) + 101, 418);
                pelletList.Add(regPellet);
                regPellet = new Pellet();
                regPellet.Position =new Vector2((i * 31) + 101, 466);
                pelletList.Add(regPellet);
                regPellet = new Pellet();
                if (i > 0 && i < 12)
                {
                    regPellet.Position =new Vector2((i * 31) + 101, 322);
                    pelletList.Add(regPellet);
                    regPellet = new Pellet();
                }
                if (i > 0 && i < 12)
                {
                    regPellet.Position =new Vector2((i * 31) + 101, 114);
                    pelletList.Add(regPellet);
                    regPellet = new Pellet();
                }
                if (i < 3 || (i > 3 && i < 9) || i > 9)
                {
                    regPellet.Position =new Vector2((i * 31) + 101, 66);
                    pelletList.Add(regPellet);
                    regPellet = new Pellet();
                }
                if (i > 2 && i < 10)
                {
                    regPellet.Position =new Vector2((i * 31) + 101, 162);
                    pelletList.Add(regPellet);
                    regPellet = new Pellet();

                }
                if (i < 4)
                {
                    regPellet.Position = new Vector2((i * 31) + 242, 210);
                    pelletList.Add(regPellet);
                    regPellet = new Pellet();
                    if (i == 0 || i == 3)
                    {
                        regPellet.Position =new Vector2((i * 31) + 242, 247);
                        pelletList.Add(regPellet);
                        regPellet = new Pellet();
                        regPellet.Position = new Vector2((i * 31) + 242, 284);
                        pelletList.Add(regPellet);
                        regPellet = new Pellet();
                    }
                }
                if ((i > 1 && i < 4) ||( i > 8 && i < 11) || i == 6)
                {
                    regPellet.Position = new Vector2((i * 31) + 101, 274);
                    pelletList.Add(regPellet);
                    regPellet = new Pellet();
                }
                if ((i > 1 && i < 4) || (i > 8 && i < 11))
                {
                    regPellet.Position = new Vector2((i * 31) + 101, 226);
                    pelletList.Add(regPellet);
                    regPellet = new Pellet();
                }
                if (i < 6)
                {
                    regPellet.Position = new Vector2(110, (i*31)+ 140);
                    pelletList.Add(regPellet);
                    regPellet = new Pellet();
                    regPellet.Position = new Vector2(465, (i * 31) + 140);
                    pelletList.Add(regPellet);
                    regPellet = new Pellet();
                }
                
            }
        }
        private void CheckPelletCollision(Vector2 pacPosition)
        {
            int removeIndex = 999;
            pacPosition.X -= 16;
            pacPosition.Y -= 16;
            foreach (Pellet a in pelletList)
            {
                Vector2 tmp = a.Position;
                tmp -= pacPosition;
                if (tmp.Length() < a.Length)
                {
                    removeIndex = pelletList.IndexOf(a);
                    break;
                }
            }
            if (removeIndex < 999)
                pelletList.RemoveAt(removeIndex);
            
        }
            
    }
}

